

#include "cocos2d.h"

#include "LoopBackGround.h"


using namespace cocos2d;


LoopBackGround::LoopBackGround()
{
	bg_a = 0;
	bg_b = 0;
}

void LoopBackGround::Init(CCNode* container, CCTexture2D*	texture, const CCPoint& pos, int interval)
{
	bg_a = new CCSprite();
	bg_a->initWithTexture(texture);
	//block->autorelease();
	//block->setOpacity(128);

	bg_a->setPosition(pos);


	CCSize s = bg_a->getContentSize();

	bg_b = new CCSprite();
	bg_b->initWithTexture(texture);
	//block->autorelease();
	//block->setOpacity(128);

	CCPoint bpos = ccpAdd(bg_a->getPosition() , ccp(s.width + interval,0));

	bg_b->setPosition(bpos);

	container->addChild(bg_a);
	container->addChild(bg_b);

	a_b_interval = interval;

	/*CCRepeatForever *repeat = CCRepeatForever::create( CCMoveBy::create(1, ccp(50,0)) );
	bg_a->runAction(repeat);
	bg_a->runAction(repeat);*/

}


void LoopBackGround::GetMove(int speed)
{
	CCRepeatForever *repeata = CCRepeatForever::create( CCMoveBy::create(1, ccp(-speed,0)) );
	bg_a->runAction(repeata);
	CCRepeatForever *repeatb = CCRepeatForever::create( CCMoveBy::create(1, ccp(-speed,0)) );
	bg_b->runAction(repeatb);

}


void LoopBackGround::Tick(float dt)
{
	if(!bg_a || !bg_b) return;

	if(bg_a->getPosition().x + bg_a->getContentSize().width < 0){
		CCPoint pos = bg_b->getPosition();
		pos = ccpAdd(pos, ccp(bg_b->getContentSize().width + a_b_interval, 0));

		bg_a->setPosition(pos);
	}

	if(bg_b->getPosition().x + bg_b->getContentSize().width < 0){
		CCPoint pos = bg_a->getPosition();
		pos = ccpAdd(pos, ccp(bg_a->getContentSize().width + a_b_interval, 0));

		bg_b->setPosition(pos);
	}


}
















